package appfuse.webapp.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import appfuse.framework.WebvegaBaseAction;
import appfuse.framework.database.SqlCommand;
import appfuse.framework.exceptions.BaseException;
import appfuse.framework.util.TokenUtil;
import appfuse.framework.util.json.*;

public class GetApPwd extends WebvegaBaseAction {	
	@Override
	public ActionForward executeAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws BaseException, JSONException, IOException
	{
		
		BufferedReader r = new BufferedReader(new InputStreamReader(request.getInputStream()));
	    String strRet = r.readLine();
		//strRet = "[{mac:\"d8:5d:4c:2d:de:66\"},{mac:\"54:e6:fc:2f:5a:a6\"},{mac:\"e0:05:c5:45:26:3e\"}]";	    
	    JSONObject objInfo = new JSONObject(strRet);
	    if (objInfo.has("token") == false ||
	    	objInfo.has("mac_list") == false)
	    {
	    	PrintWriter out = response.getWriter();
	    	out.print("{result:-2}");
	    }
	    String strUserName = "";
	    String strToken = objInfo.getString("token");
	    if (strToken.length() > 0)
	    	strUserName = TokenUtil.VaildToken(strToken);
	    JSONArray objArray = objInfo.getJSONArray("mac_list");
	    int nLen = objArray.length();
	    
	    Map<Integer, ApItemInfo> mapMac = new HashMap<Integer, ApItemInfo>();
	    for (int i = 0; i < nLen; i++)
	    {
	    	ApItemInfo itemInfo = new ApItemInfo();	     
	    	JSONObject obj = objArray.getJSONObject(i);
	    	itemInfo.strMac = obj.getString("mac");			    
		    mapMac.put(i, itemInfo);
	    }
	    boolean bRet = getApPwd(strUserName, mapMac);
	    strRet = "[";
	    for (int i = 0; i < nLen; i++)
	    {
	    	if (i > 0)
			{
	    		strRet += ",";
			}
	    	ApItemInfo itemInfo = mapMac.get(i);
	    	strRet += "{mac:\""+itemInfo.strMac+"\",pwd:\""+itemInfo.strPwd+"\"}";		    	
	    }
	    strRet += "]";
	    PrintWriter out = response.getWriter();
	    int i = 0;
	    System.out.println(strRet); 
	    out.print(strRet);			
	    return null;
	}
	private boolean getApPwd(String strUserName, Map<Integer, ApItemInfo> mapMac)
	{
		String strApPwd = "";
		String sql = "select * from ap_info where mac in(";
		for(int i = 0; i < mapMac.size(); i++)
		{
			if (i > 0)
			{
				sql += ",";
			}
			ApItemInfo itemInfo = mapMac.get(i);			
			sql += "\"" + itemInfo.strMac + "\"";			
		}
		sql += ")";
		System.out.println(sql);
		SqlCommand sqlCommand = new SqlCommand();
		try 
		{
			sqlCommand.setSqlCommand(sql);
			ResultSet rs = sqlCommand.executeQuery();
			while (rs.next()) 
			{
				String strMac = rs.getString("mac");
				String strPwd = rs.getString("pwd");
				int nShare = rs.getInt("share");
				/*
				 * 公开ap或者私有的ap才告诉用户密码
				 */
				if (nShare == 1 ||
					(nShare == 0 && isUserPrivAp(strUserName, strMac) == true))
				{
					for(int i = 0; i < mapMac.size(); i++)
					{
						ApItemInfo itemInfo = mapMac.get(i);			
						if (itemInfo.strMac.equals(strMac))
						{
							itemInfo.strPwd = strPwd;						
						}
					}				
				}
			}
		} 
		catch (SQLException ex) 
		{
			ex.printStackTrace();
		} 
		finally 
		{
			sqlCommand.close();
			sqlCommand = null;
		}
		return true;
	}
	/*
	 * 是否是用户的私有ap
	 */
	private boolean isUserPrivAp(String strUserName, String strMac)
	{
		boolean bRet = false;
		if (strUserName.length() <= 0)
		{
			return bRet;
		}
		String sql = "call sp_check_user_priv_mac(?, ?)";
		SqlCommand sqlCommand = new SqlCommand();
		try 
		{
			sqlCommand.setSqlCommand(sql);
			sqlCommand.setString(1, strUserName);
			sqlCommand.setString(2, strMac);
			ResultSet rs = sqlCommand.executeQuery();
			if (rs.next()) 
			{
				int nResult = rs.getInt("result");
				if (nResult == 1)
				{
					bRet = true;
				}
			}
		} 
		catch (SQLException ex) 
		{
			ex.printStackTrace();
		} 
		finally 
		{
			sqlCommand.close();
			sqlCommand = null;
		}
		return bRet;
	}
}
